// components/open-vip/open-vip.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notice from '../../utils/notice.js';
import utils from '../../utils/utils';
import formatDate from '../../utils/format_date';

const app = getApp();
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        visible: {
            type: Boolean,
            value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        list: []
    },

    lifetimes: {
        created: function () {
            app.getVipList().then(res => {
                let vipList = res;
                for (let i = 0; i < vipList.length; i++) {
                    if (i == 0) {
                        vipList[i].selected = true;
                        this.vip = vipList[i];
                    } else {
                        vipList[i].selected = false;
                    }
                }
                this.setData({
                    list: vipList
                })
            });

            // network.getJson(api.dictType, {
            //     type: 'vip_price'
            // }).then(res => {
            //     for (let i = 0; i < res.data.length; i++) {
            //         if (i == 0) {
            //             res.data[i].selected = true;
            //             this.vip = res.data[i];
            //         } else {
            //             res.data[i].selected = false;
            //         }
            //     }
            //     this.setData({
            //         list: res.data
            //     })
            // })
        },
        attached: function () {},
        ready: function () {},
        detached: function () {},
    },


    /**
     * 组件的方法列表
     */
    methods: {
        closeTap() {
            this.setData({
                visible: false
            })
        },

        onVisibleOpenChange(e) {
            this.setData({
                visible: e.detail.visible
            })
        },

        chooseVip(e) {
            this.vip = e.currentTarget.dataset.type;
            for (let i = 0; i < this.data.list.length; i++) {
                if (this.data.list[i].label == this.vip.label) {
                    this.data.list[i].selected = true;
                } else {
                    this.data.list[i].selected = false;
                }
            }
            this.setData({
                list: this.data.list
            })
        },

        openVip(e) {
            this.closeTap();
            utils.fromEvent().then(() => {
                network.postJson(api.openVIP, {
                    userId: app.globalData.userInfo.id,
                    amount: this.vip.value,
                    type: this.vip.label
                }).then(res => {
                    this.orderId = res.data.orderId;
                    return network.postJson(api.prepay, {
                        description: this.vip.label,
                        outTradeNo: res.data.orderNo,
                        // amount: this.vip.value,
                        amount: 0.01,
                        openid: app.globalData.userInfo.openId,
                        orderType: "3" // 1=活动，2=场地, 3=VIP
                    })
                }).then(data => {
                    let result = data.data;
                    wx.requestPayment({
                        timeStamp: result.timeStamp,
                        nonceStr: result.nonceStr,
                        package: result.packageVal,
                        signType: result.signType,
                        paySign: result.paySign,
                        success: res => {
                            this.triggerEvent('success', this.vip);
                            // let expiration = new Date(app.globalData.userInfo.expirationDate).getTime();
                            // if (expiration < new Date().getTime()) {
                            //     expiration = new Date().getTime();
                            // }
                            // if (this.vip.label == '月卡会员') {
                            //     expiration += 30 * 24 * 60 * 60 * 1000;
                            // } else if (this.vip.label == '月卡会员') {
                            //     expiration += 365 * 24 * 60 * 60 * 1000;
                            // }
                            // app.globalData.userInfo.expirationDate = formatDate.formatTime(expiration, "yyyy-MM-dd HH:mm:ss")
                            // app.globalData.userInfo.vipType = this.vip.label;
                            // this.setData({
                            //     [`userInfo.vipType`]: this.vip.label,
                            //     [`userInfo.expirationDate`]: formatDate.formatTime(expiration, "yyyy-MM-dd")
                            // })
                            // wx.reLaunch({
                            //     url: '/home/success/success?mode=VIP'
                            // })
                        },
                        fail(res) {
                            console.log(res)
                        }
                    })
                })
            })
        }

    }
})